use crypto::digest::Digest;
use crypto::mac::Mac;
use crypto::sha2::Sha256;
use crypto::md5::Md5;
use crypto::hmac::Hmac;
use rustc_serialize::hex::ToHex;
pub fn sha_256_hash(input: &str) -> String {
    let mut hasher = Sha256::new();

    // write input message
    hasher.input_str(input);
    
    // read hash digest
    let hex = hasher.result_str();

    hex
}

pub fn md5_hash(input:&str) -> String {
    let mut hasher = Md5::new();

    // write input message
    hasher.input_str(input);
    
    // read hash digest
    let hex = hasher.result_str();

    hex
}

pub fn hmac_sha256(input:&str,key:&[u8]) -> String {
    let mut hasher = Hmac::new(Sha256::new(), key);

    // write input message
    hasher.input(input.as_bytes());
    
    // read hash digest
    let v_mac_result = hasher.result();
    v_mac_result.code()[..].to_hex()
}

pub fn hmac_md5(input:&str,key:&[u8]) -> String {
    let mut hasher = Hmac::new(Md5::new(), key);

    // write input message
    hasher.input(input.as_bytes());
    
    // read hash digest
    let v_mac_result = hasher.result();
    v_mac_result.code()[..].to_hex()
}

#[cfg(test)]
mod hash_tests {
    use crate::hash_helper;
    #[test]
    fn sha_256_hash_test() {
        let result = hash_helper::sha_256_hash("adsvbsds");
        println!("{:?}",result)
    }

    #[test]
    fn md5_hash_test() {
        let result = hash_helper::sha_256_hash("adsvbsds");
        println!("{:?}",result)
    }

    #[test]
    fn hmac_md5_test() {
        let result = hash_helper::hmac_md5("adsvbsds","poprlz".as_bytes());
        println!("{:?}",result)
    }

    #[test]
    fn hmac_sha256_test() {
        let result = hash_helper::hmac_sha256("adsvbsds","poprlz".as_bytes());
        println!("{:?}",result)
    }

    
 
}
